import {getGoodsList,getSort} from "api/api"

export default {
  state:{
    goodsList:[],
    // 总条数
    total:0,
    // 品牌
    trademarkList:[],
    // 属性
    attrsList:[],
    sortList:[]
  },
  getters:{},
  mutations:{
    getGoodsList(state,{goodsList,total,trademarkList,attrsList}){
      state.goodsList = goodsList
      state.total = total
      state.trademarkList=trademarkList
      state.attrsList=attrsList
    },
    getSort(state,data){
      state.sortList = data
    }
  },
  actions:{
    async getGoodsList(store,options){
      try{
        const {data,code} = await getGoodsList(options)
        if(code===200){
          if(data.goodsList){
            store.commit("getGoodsList",{
              goodsList:data.goodsList,
              total:data.total,
              trademarkList:data.trademarkList,
              attrsList:data.attrsList
            })
            await store.dispatch("getSort")
          }
        }
        return {data,code}
      }catch(err){
        return Promise.reject(err)
      }
    },
    async getSort(store){
      try{
        const {data,code} = await getSort()
        if(code===200){
            store.commit("getSort",data)
          }
          return {data,code}
        }catch(err){
        return Promise.reject(err)
      }
    }
  }
}